package cn.pxs.service;

import cn.pxs.entity.Hot;
import cn.pxs.entity.LargeFile;
import cn.pxs.mapper.HotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

//热度业务接口实现类
@Service
public class HotServiceImpl implements HotService {
    @Autowired
    private HotMapper hotMapper;
    @Autowired
    LargeFileService largeFileService;

    @Override
    //全查询
    public List<Hot> findAll() {
        return hotMapper.findAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //删除，要删除图片和该热度
    public List<Hot> del(Integer id) {
        Hot hot = hotMapper.findById(id);//找这个图片的id
        largeFileService.delById(hot.getPicture());//删除该图片
        hotMapper.del(id);
        return hotMapper.findAll();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加热度 和 热度的图片
    public Hot addHot(LargeFile largeFile, MultipartFile file, Hot hot) {
        try{
            if(file != null){
                //图片 的操作
                String uuid = UUID.randomUUID().toString();
                largeFile.setId(uuid);
                largeFile.setFilename(file.getOriginalFilename());
                byte[] bytes = file.getBytes();
                largeFile.setContent(bytes);
                largeFileService.add(largeFile);//添加图片
                System.out.println(largeFile.getId());
            }
        }catch (Exception e){
            System.out.println(e);
        }
        hot.setPicture(largeFile.getId());
        hotMapper.addHot(hot);//添加热度图片
        return hotMapper.findById(hot.getId());
     }

    @Override
    //id查询热度
    public Hot findById(Integer id) {
       return   hotMapper.findById(id);
    }

    @Override
    //查找类型id热门
    public List<Hot> findByType(Integer num , Integer type) {
        return hotMapper.findByType(num, type);
    }
}
